package com.wuzhi.shop.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.wuzhi.shop.product.entity.AttrAttrgroupRelationEntity;
import com.wuzhi.shop.product.entity.AttrEntity;
import com.wuzhi.shop.product.service.AttrAttrgroupRelationService;
import com.wuzhi.shop.product.service.AttrService;
import com.wuzhi.shop.product.service.CategoryService;
import com.wuzhi.shop.product.vo.AttrGroupRelationVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wuzhi.shop.product.entity.AttrGroupEntity;
import com.wuzhi.shop.product.service.AttrGroupService;
import com.wuzhi.common.utils.PageUtils;
import com.wuzhi.common.utils.R;


/**
 * 属性分组
 *
 * @author gonglize
 * @email 2567007734@gmail.com
 * @date 2022-03-09 18:26:53
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    AttrService attrService;
    @Autowired
    AttrAttrgroupRelationService relationService;

    /**
     * 将属性组和属性关联
     * @return
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){

        return R.ok();
    }

    /**
     * 查出一个属性分组关联的所有属性：
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId) {

        List<AttrEntity> entities = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data", entities);
    }

    /**
     * 列表：
     * 传入一个id，得到一组数据
     */
    @RequestMapping("/list/{catelogId}")
//    @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {

        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);

        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * /11/noattr/relation
     * - 传入分页信息和组名，找出属性分组没有关联的属性。
     */
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrGroupId") Long attrGroupId,@RequestParam Map<String,Object> params) {

        PageUtils page =  attrService.getNoRelationAttr(params,attrGroupId);
        return R.ok().put("page", page);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {

        attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 删除属性分组与属性的关联关系：
     * PostMapping 不返回数据
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {

        attrService.deleteRelation(vos);
        return R.ok();
    }
}
